package com.hen.web.henweb.controller;

import cn.hutool.core.util.StrUtil;
import com.hen.web.henweb.common.Pageable;
import com.hen.web.henweb.common.R;
import com.hen.web.henweb.exception.BusinessException;
import com.hen.web.henweb.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 狠穿
 * @version 1.0.0
 * @date 2023/5/1
 * @function 文章控制器
 */

@RequestMapping("/article")
@RestController
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 发布文章
     * @param authorId 作者ID
     * @param title 标题
     * @param content 内容
     * @return R
     */
    @PostMapping("/publish/{authorId}/{title}/{content}")
    public R publishArticle(@PathVariable String authorId,
                            @PathVariable String title,
                            @PathVariable String content){
        try{
            articleService.publishArticle(title, authorId, content);
            return R.succ("文章发布成功");
        }catch (BusinessException e){
            return R.fail(StrUtil.format("文章发布失败，原因：",e.getMessage()));
        }
    }

    /**
     * 查询文章列表
     * @param pageable
     * @return
     */
    @GetMapping("/list")
    public R listArticle(Pageable pageable){
        return R.succ(articleService.listArticles(pageable));
    }
}
